package com.test;

/*
public class Example01 {
    public static void main(String[] args) {
        MyThread01 myThread = new MyThread01(); //创建MyThread01实例对象
        myThread.run();
        while(true){
            System.out.println("Main方法在运行");
        }
    }
}

class MyThread01{
    public void run(){
        while(true){
            System.out.println("MyThread类的run()方法在运行");
        }
    }
}
 */

/*
public class Example01 {
    public static void main(String[] args) {
        MyThread02 myThread = new MyThread02(); // 创建MyThread02的线程对象
        myThread.start(); // 开启线程
        while (true) { // 通过死循环语句打印输出
            System.out.println("Main()方法在运行");
        }
    }
}

class MyThread02 extends Thread {
    public void run() {
        while (true) { // 通过死循环语句打印输出
            System.out.println("MyThread类的run()方法在运行");
        }
    }
}

 */

/*
public class Example01{
    public static void main(String[] args) {
        MyThread03 myThread = new MyThread03(); //创建MyThread03类的实例对象
        Thread thread =new Thread(myThread);  //创建线程对象
        thread.start();
        while(true){
            System.out.println("Main()方法正在运行");
        }
    }
}

class MyThread03 implements Runnable {
    public void run() { // 线程的代码段,当调用start()方法时,线程从此处开始执行
        while(true){
            System.out.println("MyThread03类的run()方法在运行");
        }

    }
}

 */


/*
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//定义一个实现Callable接口的实现类
class MyThread04 implements Callable<Object> {
    //重写Callable接口的call()方法
    public Object call() throws Exception {
        int i = 0;
        while (i++ < 5) {
            System.out.println(Thread.currentThread().getName() + "的call()方法在进行");
        }
        return i;
    }
}


public class Example01 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyThread04 myThread = new MyThread04(); //创建Callable接口的实例对象
        //使用FutureTask封装MyThread04类
        FutureTask<Object> ft1 = new FutureTask<>(myThread);
        //使用Thread(Runnable target,String name)构造方法创建线程对象
        Thread thread1 = new Thread(ft1, "thread");
        //调用线程对象的start()方法启动线程
        thread1.start();
        //通过FutureTask对象的方法管理返回值
        System.out.println(Thread.currentThread().getName() + "的返回结果:" + ft1.get());
        int a = 0;
        while (a++ < 5) {
            System.out.println("main()方法正在运行");
        }
    }
}

 */

/*
public class Example01 {
    public static void main(String[] args) {
        new TicketWindow().start();// 创建并开启第一个线程对象TicketWindow
        new TicketWindow().start();// 创建并开启第二个线程对象TicketWindow
        new TicketWindow().start();
        new TicketWindow().start();

    }
}

class TicketWindow extends Thread {
    private int tickets=100;
    public void run() {
        while(tickets>0){   //通过while循环判断票数并打印语句
            Thread th=Thread.currentThread();   //获取当前线程
            String th_name=th.getName();    //获取当前线程的名字
            System.out.println(th_name+"正在发售第 "+tickets-- +" 张票");

        }
    }
}

 */

public class Example01 {
    public static void main(String[] args) {
        TicketWindow tw = new TicketWindow(); // 创建TicketWindow实例对象tw
        new Thread(tw, "窗口1").start(); // 创建线程对象并命名为窗口1，开启线程
        new Thread(tw, "窗口2").start(); // 创建线程对象并命名为窗口2，开启线程
        new Thread(tw, "窗口3").start(); // 创建线程对象并命名为窗口3，开启线程
        new Thread(tw, "窗口4").start(); // 创建线程对象并命名为窗口4，开启线程
    }
}

class TicketWindow implements Runnable {
    private int tickets = 100;
    public void run() {
        while (tickets > 0) {
            Thread th = Thread.currentThread(); // 获取当前线程
            String th_name = th.getName(); // 获取当前线程的名字
            System.out.println(th_name + " 正在发售第 " + tickets-- + " 张票 ");
        }
    }
}
